package com.github.taomus.gitoys.ftp

import org.apache.ftpserver.FtpServerFactory
import org.apache.ftpserver.listener.ListenerFactory
import org.apache.ftpserver.usermanager.impl.BaseUser
import java.util.ArrayList
import org.apache.ftpserver.ftplet.Authority
import org.apache.ftpserver.usermanager.impl.WritePermission
import com.github.taomus.gitoys.config.ConfigManager
import org.apache.ftpserver.FtpServer

class FtpToy {
	var FtpServer server
	def static main(String[] args){
		new FtpToy().start();	
	}

	def start(){
		var serverFactory = new FtpServerFactory()
		var config = ConfigManager.read
		var factory = new ListenerFactory()
		//factory.serverAddress = config.ftp.host
		factory.port = config.ftp.port
		serverFactory.addListener("default", factory.createListener())
		var user = new BaseUser()
		user.name = config.ftp.username
		user.password = config.ftp.password
		user.homeDirectory = config.ftp.path
		//替换默认监听
		var authorities = new ArrayList<Authority>() 
	    //增加写权限
	    authorities.add(new WritePermission())
	    user.authorities = authorities
	    //增加该用户
	    serverFactory.getUserManager().save(user) 
	    server = serverFactory.createServer()  
	    server.start()
	}	
	
	def stop(){
		server.stop()
	}
}